package com.sskj.level.presenter;

import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.level.bean.TradeListBean;
import com.sskj.level.bean.WinLossBean;
import com.sskj.level.http.HttpConfig;
import com.sskj.level.ui.fragment.HolderFragment;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.bean.StockCoinBean;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;


public class HolderFragmentPresenter extends BasePresenter<HolderFragment> {


    public void getTradeList() {
        httpService.getChiCang(null)
                .execute(new JsonCallBack<HttpData<PageBean<TradeListBean>>>() {
                    @Override
                    public void onSuccess(Response<HttpData<PageBean<TradeListBean>>> response) {
                        HttpData<PageBean<TradeListBean>> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.updateUI(httpData.getData().getList());
                        }

                    }

                    @Override
                    public void onError(Response<HttpData<PageBean<TradeListBean>>> response) {
                        super.onError(response);
                        mView.updateUI(new ArrayList<>());
                    }
                });
    }

    public Flowable<List<TradeListBean>> getTradeListFlow() {
        return httpService.getChiCang(null)
                .converter(new JsonConverter<HttpData<PageBean<TradeListBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(pageBeanHttpData -> pageBeanHttpData.getData().getList())
                .onErrorReturn(throwable -> new ArrayList<>())

                ;

    }

    public void getWinLoss(String pid) {
        httpService.getWinLoss(pid)
                .execute(new JsonCallBack<HttpData<WinLossBean>>() {
                    @Override
                    public void onSuccess(Response<HttpData<WinLossBean>> response) {
                        HttpData<WinLossBean> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.setStop(httpData.getData());
                        }
                    }
                });
    }

    public void pingCang(String num, String orderId) {
        httpService.pingCang(num, orderId)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        ToastUtil.showShort(httpData.getMsg());
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.pingCangSuccess();
                        }
                    }
                });
    }

    public void setStop(String orderId, String stopProfitPrice, String stopLossPrice) {
        httpService.setStop(orderId, stopProfitPrice, stopLossPrice)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        ToastUtil.showShort(httpData.getMsg());
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.refresh();
                        }
                    }
                });
    }

}
